package com.hzdl.house_mgr.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.house_mgr.bean.House;
import com.hzdl.house_mgr.bean.User;
import com.hzdl.house_mgr.service.HouseService;
import com.hzdl.house_mgr.service.UserService;
import com.hzdl.house_mgr.util.AjaxResult;
import com.hzdl.house_mgr.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpCookie;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HouseService houseService;


    @RequestMapping("userJson")
    public AjaxResult userJson(User user,Integer page,Integer limit){
        PageInfo<User> all = userService.findAll(user,page, limit);
        int count = userService.getCount(user);
        return AjaxResult.ok(count,all.getList());
    }

    @RequestMapping("userAddorUpd")
    public AjaxResult userAddorUpd(User user){
        Integer userId = user.getUserId();
        if(userId!=null){
            int i = userService.userUpd(user);
            if(i>0){
                return AjaxResult.UPD_SUCCESS();
            }else {
                return AjaxResult.UPD_FAILED();
            }
        }else {
            int i = userService.userAdd(user);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else {
                return AjaxResult.ADD_FAILED();
            }
        }
    }

    @RequestMapping("del")
    public AjaxResult del(Integer userId){
        int del = userService.del(userId);
        if(del>0){
            return AjaxResult.DEL_SUCCESS();
        }else {
            return AjaxResult.DEL_FAILED();
        }
    }

    @RequestMapping("delBatch")
    public AjaxResult delBatch(String userId){
        int i = userService.delSome(userId.split(","));
        if(i>0){
            return AjaxResult.DELSOME_SUCCESS();
        }else {
            return AjaxResult.DELSOME_FAILED();
        }
    }

    @RequestMapping("resetPwd")
    public AjaxResult resetUserPwd(Integer userId){
        int i = userService.resetUserPwd(userId);
        if(i>0){
            return AjaxResult.RESETPWD_SUCCESS();
        }else {
            return AjaxResult.RESETPWD_FAILED();
        }
    }

    @RequestMapping("autoYzm")
    public void autoYzm(HttpSession session, OutputStream os){
        String yzm = VerifyCodeUtils.generateVerifyCode(4);
        session.setAttribute("session_yzm",yzm);
        try {
            VerifyCodeUtils.outputImage(120,33,os,yzm);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("chkYzm")
    @ResponseBody
    public String chkYzm(String yzm,HttpSession session){
        Object session_yzm = session.getAttribute("session_yzm");
        if(session_yzm!=null){
            String sYzm = String.valueOf(session_yzm);
            if(yzm.equalsIgnoreCase(sYzm)){
                return "0";
            }
        }
        return "1";
    }
    @RequestMapping("chkUser")
    @ResponseBody
    public String chkUser(String loginName, String userPwd, HttpServletRequest req,HttpServletResponse resp){
        User user = userService.chkLoginNameAndUserPwd(loginName, userPwd);
        if (user != null ){

            Cookie userid = new Cookie("userid", loginName);
            Cookie password = new Cookie("password", userPwd);

            userid.setPath("/");
            password.setPath("/");

            resp.addCookie(userid);
            resp.addCookie(password);

            List<House> all = houseService.findAll();
            req.getSession().setAttribute("count",houseService.getCount());
            req.getSession().setAttribute("houses",all);
            req.getSession().setAttribute("session_user",user);


            return "0";
        }
        return "1";
    }


    //查询是否有这个用户通过账号名
    @RequestMapping("chkLoginIdExits")
    public AjaxResult chkLoginIdExits(String loginName){
        User user = userService.chkLoginIdExits(loginName);
        if(user!=null){
            return AjaxResult.FIND_SUCCESS();
        }else {
            return AjaxResult.FIND_FAILED();
        }
    }

    @RequestMapping("register")
    public AjaxResult register(User user){
        int register = userService.register(user);
        if(register>0){
            return AjaxResult.ADD_SUCCESS();
        }else {
            return AjaxResult.UPD_FAILED();
        }
    }


}
